package cz.kfad.ofrss.common;


import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
import cz.kfad.ofrss.R;
import cz.kfad.ofrss.model.RssFeedShort;

public class FeedListAdapter extends ArrayAdapter<RssFeedShort> {

	private Filter filter;

    public FeedListAdapter(Context context, List<RssFeedShort> objects) {
        super(context, R.layout.feed_list_item, objects);
		filter = new TitleIgnoreCaseFilter(objects);
    }


    private static class ViewHolder {
        public TextView textView;
        public TextView dateView;
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View itemView;
        if (convertView != null) {
            itemView = convertView;
        } else {
            final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.feed_list_item, null, true);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) itemView.findViewById(R.id.feed_list_item_text);
            viewHolder.dateView = (TextView) itemView.findViewById(R.id.feed_list_item_date);
            itemView.setTag(viewHolder);
        }

        ViewHolder viewHolder = (ViewHolder) itemView.getTag();

        final RssFeedShort rssFeed = getItem(position);
        viewHolder.textView.setText(rssFeed.getTitle());
		viewHolder.textView.setTextColor(rssFeed.isRead() ? Color.WHITE : Color.YELLOW);
		viewHolder.dateView.setTextColor(rssFeed.isRead() ? Color.WHITE : Color.YELLOW);

        final Date date = rssFeed.getDate();
        if (date != null) {
			viewHolder.dateView.setText(date.toLocaleString());
        }
        return itemView;
    }

	@Override
    public Filter getFilter() {
		return filter;
    }

	public class TitleIgnoreCaseFilter extends Filter {

		List<RssFeedShort> items;

		public TitleIgnoreCaseFilter(List<RssFeedShort> items) {
			this.items = new ArrayList<RssFeedShort>(items.size());
			for (RssFeedShort rssFeedShort : items) {
				this.items.add(rssFeedShort);
			}
			// Collections.copy(this.items, items);

		}

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults filterResults = new FilterResults();
			List<RssFeedShort> match = new LinkedList<RssFeedShort>();
			for (RssFeedShort rssFeedShort : items) {
				if (rssFeedShort.getTitle().toLowerCase().contains(constraint.toString().toLowerCase())) {
					match.add(rssFeedShort);
				}
			}
			filterResults.values = match;
			filterResults.count = match.size();
			return filterResults;
		}

		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			List<RssFeedShort> filtered = (List<RssFeedShort>) results.values;
			clear();
			for (RssFeedShort rssFeedShort : filtered) {
				add(rssFeedShort);
			}
			notifyDataSetInvalidated();
		}

	}


}
